package com.flower.subject.domain.service.impl;

import com.flower.subject.domain.convent.SubjectCategoryConverter;
import com.flower.subject.domain.entity.SubjectCategoryBO;
import com.flower.subject.domain.entity.SubjectInfoBO;
import com.flower.subject.domain.service.SubjectCategoryDomainService;
import com.flower.subject.entity.PageResult;
import com.flower.subject.enums.IsDeletedFlagEnum;
import com.flower.subject.infra.basic.entity.SubjectCategory;
import com.flower.subject.infra.basic.service.SubjectCategoryService;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-03 11:30
 */

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;


    private Cache<String,String> localCache = CacheBuilder.newBuilder()
            .maximumSize(5000)
            .expireAfterWrite(10, TimeUnit.SECONDS).build();


    public void add(SubjectCategoryBO bo) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANT.convertBoTOCategory(bo);
        subjectCategoryService.insert(subjectCategory);

    }


    public List<SubjectCategoryBO> queryPrimaryCategory(SubjectCategory subjectCategory) {

        List<SubjectCategory> list =  subjectCategoryService.queryPrimaryCategory(subjectCategory);
        List<SubjectCategoryBO> boList = SubjectCategoryConverter.INSTANT.convertBoTOCategory(list);
        if(log.isInfoEnabled()){
            log.info("SubjectCategory boList",boList);
        }
        return boList;
    }

    public List<SubjectCategoryBO> queryCateGory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANT.convertBoTOCategory(subjectCategoryBO);
        List<SubjectCategory> subjectCategories = subjectCategoryService.queryPrimaryCategory(subjectCategory);
        List<SubjectCategoryBO> boList = SubjectCategoryConverter.INSTANT.convertBoTOCategory(subjectCategories);

        return boList;
    }

    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANT.convertBoTOCategory(subjectCategoryBO);
        int count = subjectCategoryService.update(subjectCategory);
        if (count > 0){
            return true;
        }
        return false;
    }

    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANT.convertBoTOCategory(subjectCategoryBO);
        subjectCategory.setUpdateBy(IsDeletedFlagEnum.DELETE.getCode().toString());
        int count = subjectCategoryService.update(subjectCategory);

        return count > 0;
    }

    @Override
    public PageResult<SubjectInfoBO> getSubjectPage(SubjectInfoBO subjectInfoBO) {


        return null;
    }
}
